using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.Win32;
using System.Reflection;
using FreeSMS.Configuration.Sections;

namespace FreeSMS.Configuration
{
    public class Config
    {
        #region Static Members
        private static readonly string CONFIG_FILE_NAME = "config.xml";
        private static readonly string CONFIG_FILE_PATH;
        #endregion

        #region Static Methods
        public static Config LoadConfig()
        {
            Config config = null;

            try
            {
                if (!File.Exists(CONFIG_FILE_PATH))
                    throw new Exception();

                XmlSerializer ser = new XmlSerializer(typeof(Config));
                StreamReader sr = new StreamReader(CONFIG_FILE_PATH);
                config = (Config)ser.Deserialize(sr);
                sr.Close();
            }
            catch
            {
                config = GetDefaultConfig();
            }

            return config;

        }
        public static Config GetDefaultConfig()
        {
            return new Config();
        } 
        #endregion

        #region Members
        private ConfigSectionGeneral _general;
        private ConfigSectionConnection _connection; 
        #endregion

        #region Properties
        public ConfigSectionConnection Connection
        {
            get { return _connection; }
            set { _connection = value; }
        }
        public ConfigSectionGeneral General
        {
            get { return _general; }
            set { _general = value; }
        } 
        #endregion
	                       
        #region Methods
        public void SaveConfig()
        {
            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(Config));
                StreamWriter sw = new StreamWriter(CONFIG_FILE_PATH);
                ser.Serialize(sw, this);
                sw.Close();
            }
            catch (Exception e)
            {
                
            }
        }
        #endregion       

        #region Initialize
        static Config()
        {
            // init contacts file path (UAC Vista problem in program files directory)
            string appPath = Assembly.GetExecutingAssembly().Location;
            string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\FreeSMS";
            string programDirPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            //if (appPath.Contains(programDirPath))
            //{
                CONFIG_FILE_PATH = appDataPath + "\\" + CONFIG_FILE_NAME;
                if (!Directory.Exists(appDataPath))
                    Directory.CreateDirectory(appDataPath);
            //}
            //else
            //{
            //    CONFIG_FILE_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + CONFIG_FILE_NAME;
            //}                        
        }
        public Config()
        {
            _general = new ConfigSectionGeneral();
            _connection = new ConfigSectionConnection();
        }
        #endregion
    }    
}
